﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ServerSocketTest
{
    public static class LogHelper
    {
        //拼接日誌目錄
        static string appLogPath = AppDomain.CurrentDomain.BaseDirectory + "log/";
        static string ExceptionLogFileName = "SystemException_";

        static string DataLogFileName = "DataLog_";



        /// <summary>
        /// 寫入日誌
        /// </summary>
        /// <param name="ex">異常對像</param>
        public static void WriteExceptionLog(Exception ex)
        {
            //日誌目錄是否存在 不存在創建
            if (!Directory.Exists(appLogPath))
            {
                Directory.CreateDirectory(appLogPath);
            }
            StringBuilder logInfo = new StringBuilder("");
            string currentTime = System.DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss]");
            if (ex != null)
            {
                //logInfo.Append("\n");
                //logInfo.Append(currentTime + "\n");
                ////獲取描述當前的異常的信息
                //logInfo.Append(ex.Message + "\n");
                ////獲取當前實例的運行時類型
                //logInfo.Append(ex.GetType() + "\n");
                ////獲取或設置導致錯誤的應用程序或對象的名稱
                //logInfo.Append(ex.Source + "\n");
                ////獲取引發當前異常的方法
                //logInfo.Append(ex.TargetSite + "\n");
                ////獲取調用堆棧上直接楨的字符串表示形式
                //logInfo.Append(ex.StackTrace + "\n");

                logInfo.Append(System.Environment.NewLine);
                logInfo.Append(currentTime + System.Environment.NewLine);
                //獲取描述當前的異常的信息
                logInfo.Append(ex.Message + System.Environment.NewLine);
                //獲取當前實例的運行時類型
                logInfo.Append(ex.GetType() + System.Environment.NewLine);
                //獲取或設置導致錯誤的應用程序或對象的名稱
                logInfo.Append(ex.Source + System.Environment.NewLine);
                //獲取引發當前異常的方法
                logInfo.Append(ex.TargetSite + System.Environment.NewLine);
                //獲取調用堆棧上直接楨的字符串表示形式
                logInfo.Append(ex.StackTrace + System.Environment.NewLine);
            }
            System.IO.File.AppendAllText(appLogPath + ExceptionLogFileName + DateTime.Now.ToString("yyyy-MM-dd") + ".log", logInfo.ToString());
        }
        public static void WriteExceptionLog(UnhandledExceptionEventArgs args)
        {
            Exception ex = (Exception)args.ExceptionObject;
            //日誌目錄是否存在 不存在創建
            if (!Directory.Exists(appLogPath))
            {
                Directory.CreateDirectory(appLogPath);
            }
            StringBuilder logInfo = new StringBuilder("");
            string currentTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            if (ex != null)
            {
                logInfo.Append(System.Environment.NewLine);
                logInfo.Append("[DateTime] " + currentTime + System.Environment.NewLine);
                //獲取描述當前的異常的信息
                logInfo.Append("[Exception] " + ex.Message + System.Environment.NewLine);
                //獲取當前實例的運行時類型
                logInfo.Append("[Type] " + ex.GetType() + System.Environment.NewLine);
                //獲取或設置導致錯誤的應用程序或對象的名稱
                logInfo.Append("[Source] " + ex.Source + System.Environment.NewLine);
                //獲取引發當前異常的方法
                logInfo.Append("[TargetSite] " + ex.TargetSite + System.Environment.NewLine);
                //獲取調用堆棧上直接楨的字符串表示形式
                logInfo.Append("[StackTrace]\n" + ex.StackTrace + System.Environment.NewLine);

                logInfo.Append("[RuntimeTerminating] " + args.IsTerminating + System.Environment.NewLine);

            }
            System.IO.File.AppendAllText(appLogPath + ExceptionLogFileName + DateTime.Now.ToString("yyyy-MM-dd") + ".log", logInfo.ToString());
        }

        public enum MsgType { Exception, Normal, Error, Warning };
        public enum Action { ServerMessages, ClientConnected, ClientLefted, TransmittedToClient, ReceivedFromClient };
        public static void WriteServerDataLog(MsgType msgType, Action action, string ip, String description)
        {
            //日誌目錄是否存在 不存在創建
            if (!Directory.Exists(appLogPath))
            {
                Directory.CreateDirectory(appLogPath);
            }
            string currentTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            string logName = appLogPath + DataLogFileName + DateTime.Now.ToString("yyyy-MM-dd");

            //Save To Normal Log format
            string fileName = logName + ".log";
            StringBuilder logInfo = new StringBuilder("");
            logInfo.Append("[" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] [" + ip + "] ");
            switch (action)
            {
                case Action.ServerMessages:
                case Action.ClientConnected:
                case Action.ClientLefted:
                    logInfo.Append("-- ");
                    break;
                case Action.ReceivedFromClient:
                    logInfo.Append("<< ");
                    break;
                case Action.TransmittedToClient:
                    logInfo.Append(">> ");
                    break;
            }
            logInfo.Append(description + System.Environment.NewLine);    //[Source IP]

            System.IO.File.AppendAllText(fileName, logInfo.ToString());


            //Save To CSV format
            fileName = logName + ".csv";

            logInfo.Clear();

            if (!System.IO.File.Exists(fileName))
            {
                logInfo.Append("--------------------------------------" + System.Environment.NewLine);
                logInfo.Append("This is the header for the file." + System.Environment.NewLine);
                logInfo.Append("The File Name: " + fileName + System.Environment.NewLine);
                //logInfo.Append("The time of Creating is: " + System.DateTime.Now.ToString() + System.Environment.NewLine);
                logInfo.Append("The time of Creating is: " + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + System.Environment.NewLine);
                logInfo.Append("--------------------------------------" + System.Environment.NewLine);
                logInfo.Append("[Date], [Time], [MsgType], [Action], [Source IP], [Description]" + System.Environment.NewLine);
            }

            logInfo.Append(DateTime.Now.ToString("yyyy-MM-dd") + ", ");  //[Date]
            logInfo.Append(DateTime.Now.ToString("HH:mm:ss") + ", ");    //[Time]
            logInfo.Append(msgType + ", ");    //[MsgType]
            logInfo.Append(action + ", ");    //[Action]
            logInfo.Append(ip + ", ");    //[Source IP]
            logInfo.Append(description + System.Environment.NewLine);    //[Description]
            //System.IO.File.AppendAllText(appLogPath + ExceptionLogFileName + DateTime.Now.ToString("yyyy-MM-dd") + ".log", logInfo.ToString());
            System.IO.File.AppendAllText(fileName, logInfo.ToString(), Encoding.Unicode);
        }


    }//public static class LogHelper
}//namespace ServerSocketTest
